unit Button;

interface    

uses DGLE, DGLE_Types, Engine, Types;

type
  TButtonState = (bsNone, bsOver, bsDown);     

type
  TButton = class(TObject)
  private
    P: TPoint;
    FLeft: Integer;
    FTop: Integer;
    FHeight: Integer;
    FWidth: Integer;
    FCaption: AnsiString;
    FFontColor: TColor4;
    FState: TButtonState;
    pButton: array [0..2] of ITexture;
    FSellected: Boolean;
    FFontColorSellected: TColor4;
    FFontColorOver: TColor4;
    procedure SetLeft(const Value: Integer);
    procedure SetTop(const Value: Integer);
    procedure SetHeight(const Value: Integer);
    procedure SetWidth(const Value: Integer);
    procedure SetCaption(const Value: AnsiString);
    procedure SetFontColor(const Value: TColor4);
    procedure SetState(const Value: TButtonState);
    procedure SetSellected(const Value: Boolean);
    procedure SetFontColorSellected(const Value: TColor4);
    procedure SetFontColorOver(const Value: TColor4);
  public
    constructor Create();
    destructor Destroy; override;
    property Top: Integer read FTop write SetTop;
    property Left: Integer read FLeft write SetLeft;
    property Width: Integer read FWidth write SetWidth;
    property Height: Integer read FHeight write SetHeight;
    property Caption: AnsiString read FCaption write SetCaption;
    property FontColorOver: TColor4 read FFontColorOver write SetFontColorOver;
    property FontColorDefault: TColor4 read FFontColor write SetFontColor;
    property FontColorSellected: TColor4 read FFontColorSellected write SetFontColorSellected;
    property State: TButtonState read FState write SetState;
    property Sellected: Boolean read FSellected write SetSellected;
    function MouseOver(): Boolean;
    function Click(): Boolean;
    procedure Render();
    procedure Update();
  end;


implementation

uses SysUtils, uBox;

{ TButton }

function TButton.Click(): Boolean;
begin
  Result := LeftButton and MouseOver;
  if Result then State := bsDown else State := bsNone;
end;

constructor TButton.Create();
var
  I: Byte;
begin
  P := Point(0, 0);
  for I := 0 to High(pButton) do pButton[I] := nil;
  State := bsNone;
  Sellected := False;

  for I := 0 to High(pButton) do
    pResMan.Load(PAnsiChar(AnsiString('Resources\Sprites\Interface\Button' + IntToStr(I) + '.png')), IEngineBaseObject(pButton[I]), TEXTURE_LOAD_DEFAULT_2D);
  FontColorSellected := Color4();
  FontColorDefault := Color4();
  FontColorOver := Color4();
end;

destructor TButton.Destroy;
var
  I: Byte;
begin
  for I := 0 to High(pButton) do pButton[I] := nil;    
  inherited;
end;

function TButton.MouseOver: Boolean;
begin
  Result := (MousePos.X > Left) and (MousePos.X < Left + Width) and (MousePos.Y > Top) and (MousePos.Y < Top + Height);
end;

procedure TButton.Render;

  procedure RenderButton(B: ITexture; C: TColor4);
  var
    W, H, X, Y: Cardinal;
  begin
    pRender2D.DrawTexture(B, Point2(Left + P.X, Top + P.Y), Point2(Width, Height));
    pFont24.GetTextDimensions(PAnsiChar(Caption), W, H);
    X := ((Width div 2) - (Integer(W) div 2)) + (Left + P.X);
    Y := ((Height div 2) - (Integer(H) div 2)) + (Top + P.Y);
    pFont24.Draw2DSimple(X, Y, PAnsiChar(Caption), C);
  end;

begin
  if (State <> bsDown) then
    if MouseOver and not FSellected then
      State := bsOver else State := bsNone;
  if (State = bsDown) and not MouseOver then State := bsNone;
  case State of
    bsNone: if FSellected then
              RenderButton(pButton[2], FontColorSellected)
                else RenderButton(pButton[0], FontColorDefault);
    bsOver: RenderButton(pButton[1], FontColorOver);
    bsDown: if FSellected then
              RenderButton(pButton[2], FontColorSellected)
                else RenderButton(pButton[1], FontColorOver);
  end;
end;

procedure TButton.Update;
begin
  if Click then P := Point(1, 3) else P := Point(0, 0);
end;

procedure TButton.SetCaption(const Value: AnsiString);
begin
  FCaption := Value;
end;

procedure TButton.SetFontColor(const Value: TColor4);
begin
  FFontColor := Value;
end;

procedure TButton.SetHeight(const Value: Integer);
begin
  FHeight := Value;
end;

procedure TButton.SetLeft(const Value: Integer);
begin
  FLeft := Value;
end;

procedure TButton.SetSellected(const Value: Boolean);
begin
  FSellected := Value;
end;

procedure TButton.SetState(const Value: TButtonState);
begin
  FState := Value;
end;

procedure TButton.SetTop(const Value: Integer);
begin
  FTop := Value;
end;

procedure TButton.SetWidth(const Value: Integer);
begin
  FWidth := Value;
end;

procedure TButton.SetFontColorSellected(const Value: TColor4);
begin
  FFontColorSellected := Value;
end;

procedure TButton.SetFontColorOver(const Value: TColor4);
begin
  FFontColorOver := Value;
end;

end.
